/**

 @Name：layim
 @Author：wzt
 @Site：http://layim.layui.com
 @License：LGPL

 */
layui.define(['layer','layim','contextMenu','upload','form'],function(exports){
    var $=layui.jquery,layer=layui.layer,layim=layui.layim,contextMenu = layui.contextMenu,upload = layui.upload,form=layui.form;
    var cachedata =  layui.layim.cache();
    //全局方法   变量
    var imindex={
        usersIds:[]//新建群聊用户id组
        ,groupids:[]//右键菜单群组id权限
        ,msgboxIndex:0 //系统新消息提醒数量统计
        //右键菜单功能
        //1打开群聊菜单
        ,menugroupchat:function() {
            return data= { // 菜单项
                text: "打开群聊",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name,
                        group_avatar = othis[0].dataset.img;
                    layim.chat({
                        name: group_name
                        ,type: 'group'
                        ,avatar: group_avatar
                        ,id: group_id
                    });
                }
            }
        }
        //2添加群组成员
        ,menuaddgrouppen:function () {
            return data={
                text: "添加群组成员",
                icon: "layui-icon layui-icon-add-1",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name;
                    events.addGroupPen(group_id,group_name)
                }
            }
        }
        //3删除群组成员
        ,menudelgrouppen:function () {
            return data= {
                text: "删除群组成员",
                icon: "layui-icon layui-icon-delete",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name;
                    events.delGroupPen(group_id,group_name)

                }
            }
        }
        //4解散群组
        ,menudelgroup:function () {
            return data= {
                text: "解散群组",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name;
                    events.dissolvegroupChat(group_id,group_name);
                }
            }
        }
        //5退出群组
        ,menuoutgroup:function () {
            return data= {
                text: "退出群组",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name;
                    events.outGroup(group_id)
                }
            }
        }
        //6群组设置
        ,menugroupconfig:function () {
            return data= {
                text: "群组设置",
                icon: "iconfont iconicon_shezhi",
                callback: function(ele) {
                    var othis = ele.parent(),
                        group_id = othis[0].dataset.id.replace(/^layim-group/g, ''),
                        group_name = othis[0].dataset.name;
                    events.groupSetups(group_id);
                }
            }
        }
        ,contextMenuconfig: function(){//定义右键操作
                // $(".layim-list-friend >li > ul > li").contextMenu({//用户右键功能
                //     width: 140, // width
                //     itemHeight: 30, // 菜单项height
                //     bgColor: "#fff", // 背景颜色
                //     color: "#333", // 字体颜色
                //     fontSize: 15, // 字体大小
                //     hoverBgColor: "#009bdd", // hover背景颜色
                //     hoverColor: "#fff", // hover背景颜色
                //     target: function(ele) { // 当前元素
                //         var id=$(ele).attr('class').split(' ')[0];
                //         $(".ul-context-menu").attr("data-id",id);
                //         $(".ul-context-menu").attr("data-name",ele.find("span").html());
                //         $(".ul-context-menu").attr("data-img",ele.find("img").attr('src'));
                //     },
                //     menu: [
                //         { // 菜单项
                //             text: "发送消息",
                //             icon: "",
                //             nav: "moreMove",//子导航的样式
                //             navIcon: "&#xe602;",//子导航的图标
                //             navBody: '<ul><li class="ui-move-menu-item" data-groupidx="groupidx"><a href="javascript:void(0);"><span>groupName</span></a></li></ul>',//子导航html
                //             callback: function(ele) {
                //
                //             }
                //         },
                //         {
                //             text: "查看资料",
                //             icon: "",
                //             callback: function(ele) {
                //                 var othis = ele.parent(),friend_id = othis[0].dataset.id.replace(/^layim-friend/g, '');
                //                 im.getInformation({
                //                     id: friend_id,
                //                     type:'friend'
                //                 });
                //             }
                //         },
                //         {
                //             text: "聊天记录",
                //             icon: "",
                //             callback: function(ele) {
                //                 var othis = ele.parent(),
                //                     friend_id = othis[0].dataset.id.replace(/^layim-friend/g, ''),
                //                     friend_name = othis[0].dataset.name;
                //                 im.getChatLog({
                //                     name: friend_name,
                //                     id: friend_id,
                //                     type:'friend'
                //                 });
                //             }
                //         }
                //     ]
                // });

            if($(".layim-list-group >li").hasClass('layim-null')){   //如果没有群   则不显示右键
               return false;
            }

            $.ajax({//获取群组权限
                url: serverPath.imPath + "/im/imRoom/getCurrentOwnGroupId",
                type: "get",
                contentType: 'application/json',
                dataType: 'json',
                success: function (data) {
                    imindex.groupids=data.data;
                }
            });

            $(".layim-list-group >li").mousedown(function(e){ //群组右键功能
                var data={ //群组右键功能
                    width: 140, // width
                    itemHeight: 30, // 菜单项height
                    bgColor: "#fff", // 背景颜色
                    color: "#333", // 字体颜色
                    fontSize: 15, // 字体大小
                    hoverBgColor: "#009bdd", // hover背景颜色
                    hoverColor: "#fff", // hover背景颜色
                    target: function(ele) { // 当前元素
                        var id=$(ele).attr('class').split(' ')[0];
                        $(".ul-context-menu").attr("data-id",id);
                        $(".ul-context-menu").attr("data-name",ele.find("span").html());
                        $(".ul-context-menu").attr("data-img",ele.find("img").attr('src'));
                    },
                    menu: []
                };
                var groupid=$(this).attr('class').split(' ')[0];//获取当前群组id
                var newgroupid=groupid.replace(/^layim-group/g, '');
                data.menu.push(imindex.menugroupchat());
                data.menu.push(imindex.menugroupconfig());
                data.menu.push(imindex.menuaddgrouppen());
                var flag=false;
                if (imindex.groupids.length>0){
                    for (var i=0;i<imindex.groupids.length;i++){
                        if(imindex.groupids[i]==newgroupid){
                            flag=true;
                        }
                    }
                }
                if(flag){
                    data.menu.push(imindex.menudelgrouppen());
                    data.menu.push(imindex.menudelgroup());
                }else{
                    data.menu.push(imindex.menuoutgroup());
                }
                $(this).contextMenu(data);  //面板群组右键事件
            })
         }

        //删除好友或退出群后  所有的操作
        ,removegroupfriend:function (data) {
            layim.removeList({//从好友或者群列表移除
                type: data.type
                ,id: data.id
            });
        }
    };

    //事件
    var events=imindex.events={
        //扩展面板功能
        extendFun:function (othis) {
            var html=$('#ccpimcont-imfunctions').html();
            events.extendFun.index=layer.tips(html, this, {
                tips: 1
                ,time: 0
                ,anim: 5
                ,fixed: true
                ,skin: 'layui-box layui-layim-contextmenu ccpim-contentMenu'
                ,success: function(layero){
                    var stopmp = function(e){ layui.stope(e); };
                    layero.off('mousedown', stopmp).on('mousedown', stopmp);
                }
            });
            function hide(){
                layer.close(events.extendFun.index);
            }
            $(document).off('mousedown', hide).on('mousedown', hide);
            $(window).off('resize', hide).on('resize', hide);
        }

        //群组功能扩展
        ,groupOperation:function (othis,e) {
            layui.stope(e);
            var html=$('#ccpimcont-groupfunctions').html();
            events.groupOperation.index=layer.tips(html, this, {
                tips: 3
                ,time: 0
                ,anim: 5
                ,fixed: true
                ,skin: 'layui-box layui-layim-contextmenu ccpim-contentMenu'
                ,success: function(layero){
                    var stopmp = function(e){ layui.stope(e); };
                    layero.off('mousedown', stopmp).on('mousedown', stopmp);
                }
            });
            function hide(){
                layer.close(events.groupOperation.index);
            }
            $(document).off('mousedown', hide).on('mousedown', hide);
            $(window).off('resize', hide).on('resize', hide);
        }

        //新建群聊功能(增删改查)
        //群组数据处理
        ,creatGroupAjax:function () {
            $('.mainPanelSelectPen').val('');
            $.ajax({
                url:serverPath.imPath +"/im/mainPanelInit?type=3",
                type: "get",
                contentType: 'application/json',
                dataType: 'json',
                success: function(data) {
                    var datas = data.data;
                    var zNodes=[];
                    for (var i=0;i<datas.friend.length;i++){
                        var childrens=datas.friend[i].list;
                        var dataChildren=[];
                        for (var j=0;j<childrens.length;j++){
                            dataChildren.push({
                                icon:childrens[j].avatar,
                                id:childrens[j].id,
                                name:childrens[j].username
                            });
                        }
                        zNodes[i]={
                            name:datas.friend[i].groupname,
                            children:dataChildren
                        }
                    }
                    var setting = {
                        data: {
                            simpleData: {
                                enable: true
                            }
                        },
                        view: {
                            showLine: false
                        },
                        callback: {
                            onClick: onClick
                        }
                    };
                    $.fn.zTree.init($('#mainPanelSelect'), setting, zNodes);
                    var treeObj = $.fn.zTree.getZTreeObj('mainPanelSelect');
                    treeObj.expandAll(true);
                    component.fuzzySearch('mainPanelSelect','.mainPanelSelectPen',null,true); //初始化模糊搜索方法
                    function onClick(event, treeId, treeNode, clickFlag) {
                        if (treeNode.children) {
                            return false;
                        }
                        for(i=0;i<imindex.usersIds.length;i++) {
                            if (imindex.usersIds[i].id==treeNode.id){
                                return false;
                            }
                        }
                        var treename='';
                        if(treeNode.oldname){
                            treename=treeNode.oldname;
                        }else{
                            treename=treeNode.name;
                        }
                        imindex.usersIds.push({id: treeNode.id, name: treename});
                        $('.groupPersonnels').append('<li><div style="background-image:url(' + treeNode.icon + ')"></div><span>' + treename + '</span><i class="layui-icon layui-icon-close" id="' + treeNode.id + '" ccpim-event="canGroupPen" style="margin-left: 5px;cursor: pointer"></i></li>')
                    }
                },
            });
        }
        //创建群聊
        ,creatGroup:function () {
            layer.close(events.groupOperation.index);
            events.creatGroup.index = layer.open({
                type: 1
                ,title: '新建群聊'
                ,id: 'creatGroupid'
                ,maxmin: false
                ,shade: false
                ,area: ['520px', '460px']
                ,btn: ['创建','取消']
                ,skin: 'layui-box layui-layer-border'
                ,resize: false
                ,content: $('#ccpimcont-creatGroups')
                ,success:function () {
                    events.creatGroupAjax();
                }
                ,yes: function(index, layero){
                    var ids=[];
                    var names=[];
                    for (var i=0;i<imindex.usersIds.length;i++){
                        ids.push(imindex.usersIds[i].id);
                        names.push(imindex.usersIds[i].name)
                    }
                    var groupName=names.join('、').slice(0,22);
                    var data = {};
                    data.initUserIds = ids;
                    data.initUserNames = names;
                    data.roomName = groupName;
                    data.avatar = serverPath.imPath + staticPath.imPath+"pictures/roomDefault.jpg";
                    $.ajax({
                          url: serverPath.imPath + "/im/imRoom/save",
                          type: "post",
                          contentType: 'application/json',
                          dataType: 'json',
                          data: JSON.stringify(data),
                          success: function (result) {
                             if (result.code === 0) {
                                     layim.addList({
                                         type: 'group' //列表类型，只支持friend和group两种
                                         ,avatar: result.data.avatar //群组头像
                                         ,groupname: result.data.roomName //群组名称
                                         ,id: result.data.id //群组id
                                     });
                                 layim.chat({
                                     name: result.data.roomName
                                     ,type: 'group'
                                     ,avatar: result.data.avatar
                                     ,id: result.data.id
                                 });
                                     imindex.contextMenuconfig();
                             } else {
                                 layer.alert(result.msg,function (index) {
                                     layer.close(index);
                                 });
                             }
                          }
                     });
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                }
                ,btn2:function (index, layero) {
                    layer.close(index);
                }
                ,end:function(){
                    imindex.usersIds=[];
                    $('.groupPersonnels').empty();
                }
            });
        }

        //用户信息
        ,userinfos:function () {
            layer.close(events.extendFun.index);
            var usernamedom='';//用户名
            var signdom='';//个性签名
            var imgdom='';//用户头像
            events.userinfos.index = layer.open({
                type: 2
                ,title: '用户信息'
                ,id: 'userinfosid'
                ,maxmin: false
                ,shade: false
                ,area: ['720px', '520px']
                ,btn: ['完成','取消']
                ,skin: 'layui-box layui-layer-border'
                ,resize: false
                ,content: serverPath.imPath+ '/im/imUser/view/userInfo?userId='+currentUserId
                ,yes: function(index, layero){
                    var submit = layero.find('iframe').contents().find("#userinfosubmit");
                    usernamedom = layero.find('iframe').contents().find(".userNameisshow");
                    signdom = layero.find('iframe').contents().find(".signNameisshow");
                    imgdom = layero.find('iframe').contents().find(".pictureUrlpath");
                    submit.trigger('click');
                }
                ,btn2:function (index, layero) {
                    layer.close(index);
                }
                ,end:function(){
                    if(usernamedom&&signdom&&imgdom){
                        var userid='layim-friend'+currentUserId;
                        var username=usernamedom.val();
                        var sign=signdom.val();
                            //同步修改数据到本地缓存
                        cachedata.mine.username=username;
                        cachedata.mine.avatar=imgdom.val();
                        layui.each(cachedata.friend, function(index, item){
                            var flag=false;
                            layui.each(item.list, function(index, item){
                                if(item.id==currentUserId){
                                    item.name=username;
                                    item.avatar=imgdom.val();
                                    item.sign=sign;
                                    flag=true;
                                    return true;
                                }
                            });
                            if(flag){
                                return true;
                            }
                        });

                        //同步历史会话记录数据到本地缓存
                        var roomid='friend'+currentUserId;
                        var local = layui.data('layim')[cachedata.mine.id];
                        var history = local.history;
                        if(history[roomid]){
                            history[roomid].name=username;
                            history[roomid].avatar=imgdom.val();
                            history[roomid].sign=sign;
                            local.history = history;
                            layui.data('layim', {
                                key: cachedata.mine.id
                                ,value: local
                            });
                        }

                        //同步群组名称到历史会话里面
                        $('.layim-list-history li.'+userid+' span').eq(0).html(username);
                        $('.layim-list-history li.'+userid+' img').attr('src',imgdom.val());
                        $('.layim-list-history li.'+userid+' p').html(sign);

                        //同步用户名  聊天面板   主面板
                        $('.layui-layim-main .layui-layim-info div.layui-layim-user').html(username);
                        var usernamesel=$('.layim-chat-friend .layim-chat-title img.'+userid).parent().find('.layim-chat-username');
                        usernamesel.find('span').html(username);
                        $('ul.layim-chat-list li.'+userid +' span').html(username);
                        $(".layim-list-friend >li > ul > li."+userid+' span').eq(0).html(username);

                        //同步用户个性签名  聊天面板   主面板
                        $('.layui-layim-main .layui-layim-info .layui-layim-remark').val(sign);
                        $(".layim-list-friend >li > ul > li."+userid+' p').html(sign);

                        //同步用户头像  聊天面板   主面板
                        $('.layim-chat-friend .layim-chat-title .layim-chat-other img.'+userid).attr('src',imgdom.val());
                        $(".layim-list-friend >li > ul > li."+userid+' img').attr('src',imgdom.val());
                        $('ul.layim-chat-list li.'+userid +' img').attr('src',imgdom.val());
                    }
                }
            });
        }

        //群聊添加删除人员  roomId群组id   group_name弹窗title
        ,addGroupPen:function (roomId,group_name) {
            events.addGroupPen.index = layer.open({
                type: 2
                ,title:'添加人员'+'--'+group_name
                ,maxmin: false
                ,shade: false
                ,id:'addgroup'+roomId
                ,area: ['520px', '460px']
                ,btn: ['添加','取消']
                ,skin: 'layui-box layui-layer-border imaddGroupPen'
                ,resize: false
                ,content:serverPath.imPath+ '/im/imRoomUser/view/add?roomId='+roomId
                ,yes: function(index, layero){
                    var submit = layero.find('iframe').contents().find(".addcreatGroupsData");
                    submit.click();
                }
                ,btn2:function (index, layero) {
                    layer.close(index);
                }
            });
        }
        //群聊删除人员    roomId群组id
        ,delGroupPen:function (roomId,group_name) {
            events.delGroupPen.index = layer.open({
                type: 2
                ,title: '删除人员'+'--'+group_name
                ,maxmin: false
                ,shade: false
                ,id:'delgroup'+roomId
                ,area: ['520px', '460px']
                ,btn: ['删除','取消']
                ,skin: 'layui-box layui-layer-border imdelGroupPen'
                ,resize: false
                ,content: serverPath.imPath+ '/im/imRoomUser/view/del?roomId='+roomId
                ,yes: function(index, layero){
                    var submit = layero.find('iframe').contents().find(".delcreatGroupsData");
                    submit.click();
                }
                ,btn2:function (index, layero) {
                    layer.close(index);
                }
            });
        }
        //解散群聊
        ,dissolvegroupChat:function (group_id,group_name) {
            layer.alert('确认解散('+group_name+')吗?', function(index){
                layer.close(index);
                $.ajax({
                    url: serverPath.imPath + "/im/imRoom/del",
                    type: "get",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: {roomId:group_id},
                    success: function (result) {
                        if (result.code === 0) {
                                layim.removeList({
                                    type: 'group'
                                    ,id: group_id
                                });
                            // var groupid='layim-group'+group_id;     //群聊解散关闭聊天页面
                            //     if($("ul.layim-chat-list").children().length>1){
                            //         $('ul.layim-chat-list li.'+groupid +' i').click();
                            //     }else{
                            //         $('.layim-chat-box .layim-chat-footer .layim-chat-bottom span.layim-send-close').click()
                            //     }
                        } else {
                            layer.alert(result.msg,function (index) {
                                layer.close(index);
                            });
                        }
                    }
                })
            });
        }
        //取消群聊已选人员
        ,canGroupPen:function(){
            var id= $(this).attr('id');
            $(this).parent().remove();
            for (var i=0;i<imindex.usersIds.length;i++){
                if(imindex.usersIds[i].id==id){
                    imindex.usersIds.splice(i,1)
                }
            }
        }

        //删除好友或退出群后清除历史记录   data为json  参数位type加id   如 'layim-group'+groupid
        ,removeHistory: function(data){
                var history = cachedata.local.history;
                delete history[data.type+data.id];
                cachedata.local.history = history;
                layui.data('layim', {
                    key: cachedata.mine.id
                    ,value: cachedata.local
                });
                $('.layim-list-history li.'+data.id).remove();
                var none = '<li class="layim-null">暂无历史会话</li>';
                if($('.layim-list-history').children().length === 0){
                    $('.layim-list-history').html(none);
                }
         }

         //退出群组
        ,outGroup:function (roomid) {
            var userIds=[];
            userIds.push(currentUserId);
            layer.confirm('确定退出当前群组吗？', function(index) {
                layer.close(index);
                $.ajax({
                    url: serverPath.imPath + "/im/imRoomUser/delRoomUsers",
                    type: "post",
                    contentType: 'application/json',
                    dataType: 'json',
                    data: JSON.stringify({userIds:userIds,roomId:roomid,signOut:true}),
                    success: function (result) {
                        if(result.code=='0'){
                                layim.removeList({
                                    type: 'group'
                                    ,id: roomid
                                });
                            // var groupid='layim-group'+roomid;
                            // if($("ul.layim-chat-list").children().length>1){
                            //     $('ul.layim-chat-list li.'+groupid +' i').click();
                            // }else{
                            //     $('.layim-chat-box .layim-chat-footer .layim-chat-bottom span.layim-send-close').click()
                            // }
                        }else{
                            layer.alert(result.msg,function (index) {
                                layer.close(index);
                            });
                        }
                    }
                });
            })
        }
        //群组设置
        ,groupSetups:function (roomid,roomname) {
            var imgPathdom='';
            var groupnamedom='';
            var isgroup='';//判断群组是否存在
            events.groupSetups.index = layer.open({
                type: 2
                ,title: '群聊设置'
                ,id: 'groupSetups'+roomid
                ,maxmin: false
                ,shade: false
                ,area: ['720px', '520px']
                ,btn: ['完成','取消']
                ,skin: 'layui-box layui-layer-border'
                ,resize: false
                ,content: serverPath.imPath+ '/im/imRoom/view/groupInfo?roomId='+roomid
                ,yes: function(index, layero){
                    var submit = layero.find('iframe').contents().find("#groupinfosubmit");
                     imgPathdom = layero.find('iframe').contents().find("#pictureUrl");
                     groupnamedom = layero.find('iframe').contents().find(".groupNameisshow");
                    isgroup = layero.find('iframe').contents().find("#isgroup");
                    submit.trigger('click');
                }
                ,btn2:function (index, layero) {
                    layer.close(index);
                }
                ,end:function(){
                    if(isgroup&&isgroup.val()=='1'){//判断群组是否存在
                        return false;
                    }
                    if(groupnamedom&&imgPathdom){
                        events.synchgroupInfor({
                            id:roomid    //群组id
                           ,name: groupnamedom.val()   //群组名称
                           ,avatar:imgPathdom.val()   //群组头像
                        })
                    }
                }
            });
        }

        //同步修改用户信息到本地
        ,synchuserInfor:function (data) {
            var userid='layim-friend'+data.id;
            var cachedatauser =  layui.layim.cache();
            //同步修改数据到本地缓存
            layui.each(cachedatauser.friend, function(index, item){
                layui.each(item.list, function(index, item){
                    if(item.id==data.id){
                        item.username=data.username;
                        item.avatar=data.avatar;
                        item.sign=data.sign;
                        return true;
                    }
                });
            });

            //同步历史会话记录数据到本地缓存
            var roomid='friend'+data.id;
            var local = layui.data('layim')[cachedatauser.mine.id];
            var history = local.history;
            if(history&&history[roomid]){
                history[roomid].name=data.username;
                history[roomid].username=data.username;
                history[roomid].avatar=data.avatar;
                history[roomid].sign=data.sign;
                local.history = history;
                layui.data('layim', {
                    key: cachedatauser.mine.id
                    ,value: local
                });
            }

            //同步群组名称到历史会话里面
            $('.layim-list-history li.'+userid+' span').eq(0).html(data.username);
            $('.layim-list-history li.'+userid+' img').attr('src',data.avatar);
            $('.layim-list-history li.'+userid+' p').html(data.sign);

            //同步用户名  聊天面板   主面板
            var usernamesel=$('.layim-chat-friend .layim-chat-title img.'+userid).parent().find('.layim-chat-username');
            usernamesel.find('span').html(data.username);
            $('ul.layim-chat-list li.'+userid +' span').html(data.username);
            $(".layim-list-friend >li > ul > li."+userid+' span').eq(0).html(data.username);

            //同步用户个性签名  聊天面板   主面板
            $(".layim-list-friend >li > ul > li."+userid+' p').html(data.sign);

            //同步用户头像  聊天面板   主面板
            $('.layim-chat-friend .layim-chat-title .layim-chat-other img.'+userid).attr('src',data.avatar);
            $(".layim-list-friend >li > ul > li."+userid+' img').attr('src',data.avatar);
            $('ul.layim-chat-list li.'+userid +' img').attr('src',data.avatar);
        }

        //同步修改群聊信息到本地
        ,synchgroupInfor:function (data) {
            var groupid='layim-group'+data.id;
            //同步修改数据到本地缓存
            layui.each(cachedata.group, function(index, item){
                if(item.id==data.id){
                    item.name=data.name;
                    item.groupname=data.name;
                    item.avatar=data.avatar;
                    return true;
                }
            });

            //同步历史会话记录数据到本地缓存
            var roomid='group'+data.id;
            var local = layui.data('layim')[cachedata.mine.id];
            var history = local.history;
            if(local&&history[roomid]){
                history[roomid].name=data.name;
                history[roomid].groupname=data.name;
                history[roomid].avatar=data.avatar;
                local.history = history;
                layui.data('layim', {
                    key: cachedata.mine.id
                    ,value: local
                });
            }

            //同步群组名称到历史会话里面
            $('.layim-list-history li.'+groupid+' span').eq(0).html(data.name);
            $('.layim-list-history li.'+groupid+' img').attr('src',data.avatar);

            //同步群组名称    聊天面板   主面板   修改layim.js 255行 群名称加标签
            $(".layim-list-group li."+groupid+' span').eq(0).html(data.name);
            $('ul.layim-chat-list li.'+groupid +' span').html(data.name);
            var grouptitdom=$('.layim-chat-group .layim-chat-other img.'+groupid).parent().find('.layim-chat-username');
            grouptitdom.find('span').html(data.name);
            //同步群组头像  聊天面板   主面板
            $(".layim-list-group li."+groupid+' img').attr('src',data.avatar);
            $('.layim-chat-group .layim-chat-title .layim-chat-other img.'+groupid).attr('src',data.avatar);
            $('ul.layim-chat-list li.'+groupid +' img').attr('src',data.avatar);
        }
    };

    //事件触发
    $('body').on('click', '*[ccpim-event]', function(e){
        var othis = $(this), methid = othis.attr('ccpim-event');
        events[methid] ? events[methid].call(this, othis, e) : '';
    });

    //输出test接口
    exports('imindex', imindex);
});